package com.mi.rof.user.controller;

import com.mi.rof.engineer.service.EngineerService;
import com.mi.rof.entity.enums.RoleEnum;
import com.mi.rof.user.service.UserService;
import com.mi.rof.utils.UserThreadLocal;
import com.mi.rof.entity.Result;
import com.mi.rof.entity.param.UserParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author zwq
 */
@RestController
@RequestMapping("/user")
@Api("用户管理")
public class UsersController {

    @Resource
    private UserService userService;

    @Resource
    private EngineerService engineerService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<String> login(@RequestBody UserParam userParam) {
        boolean isNormalUser = userParam.getRole().equals(RoleEnum.NORMAL_USER.getMsg());
        if (isNormalUser) {
            return userService.login(userParam);
        }else {
            return engineerService.login(userParam);
        }

    }

    @GetMapping("/logout")
    @ApiOperation("登出")
    public Result<String> logout() {
        stringRedisTemplate.delete(String.valueOf(UserThreadLocal.get().getUserId()));
        UserThreadLocal.remove();
        return Result.successResult("退出成功");
    }
}
